package com.wtwd.wtwddevice.listener.appMsg;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.*;
import com.wtwd.device.model.SetAlarm;
import com.wtwd.device.model.SetFrequency;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.device.util.StringUtils;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.DeviceAlarmInfo;
import com.wtwd.wtwddevice.mq.PushAppMsgService;
import com.wtwd.wtwddevice.service.IDeviceAlarmInfoService;
import com.wtwd.wtwddevice.service.IDeviceSendService;
import com.wtwd.wtwddevice.service.IDeviceService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@MsgType(MsgTypeEnum.APP_SET_ALARM_CLOCK)
@Component
public class AppSetAlarmClock implements IMsgHandler {

    @Resource
    private IDeviceSendService deviceSendService;

    @Resource
    private PushAppMsgService msgService;

    @Resource
    private IDeviceAlarmInfoService deviceAlarmInfoService;

    @Resource
    private IDeviceService deviceService;


    @Override
    public void handleMsg(MessageBase messageBase) {
        AppProtocol appProtocol = messageBase.getAppProtocol();
        JSONArray data = (JSONArray) appProtocol.getData();
        List<SetAlarm> setAlarms = data.toJavaList(SetAlarm.class);
        String body = "";
        for (int i = 0; i < setAlarms.size(); i++) {
            if (i == 0) {
                body += setAlarms.get(i).serialization();
            } else {
                body += SeparatorConstant.CMD_BODY_SEGMENTATION + setAlarms.get(i).serialization();
            }
        }
        Long deviceId = deviceService.getDeviceIdByImei(appProtocol.getImei());
        String number = deviceSendService.generateNumber(appProtocol.getImei());
        WTProtocol wtProtocol = WTProtocol.builder()
                .imei(appProtocol.getImei())
                .cmdName(MsgTypeEnum.SET_CLOCK_ACTION.getCmdName())
                .body(StringUtils.isEmpty(body) ? "0" : body)
                .bodyLength(body.length())
                .number(number)
                .build();
        if (deviceSendService.sendMsgDevice(wtProtocol)) {//如果为fase直接告知app端设置失败
            List<DeviceAlarmInfo> deviceAlarmInfos = new ArrayList<>();
            for (SetAlarm setAlarm : setAlarms) {
                DeviceAlarmInfo deviceAlarmInfo = new DeviceAlarmInfo();
                BeanUtils.copyProperties(setAlarm, deviceAlarmInfo);
                deviceAlarmInfo.setState(CmdStateEnum.SENDING.getState());
                deviceAlarmInfo.setNumber(number);
                deviceAlarmInfo.setDeviceId(deviceId);
                deviceAlarmInfo.setIsRemove(Constant.NORMAL);
                deviceAlarmInfos.add(deviceAlarmInfo);
            }
            deviceAlarmInfoService.synchronization(deviceId, number, deviceAlarmInfos);
        } else {
            AppProtocol protocol = AppProtocol.builder()
                    .imei(wtProtocol.getImei())
                    .cmdName(MsgTypeEnum.APP_SET_ALARM_CLOCK.getCmdName())
                    .state(AnswerStateEnum.ABNORMAL.getState())
                    .data(null)
                    .build();
            msgService.pushToDevice(messageBase.getForm(),
                    MessageBase.builder()
                            .appProtocol(protocol)
                            .build()
            );
        }
    }

    public static void main(String[] args) {

        List<SetAlarm> setAlarms = new ArrayList<>();
        SetAlarm setAlarm = new SetAlarm();
        setAlarm.setCycle("0+1+2");
        setAlarm.setTime("09:20");
        setAlarm.setFlag(1);
        setAlarm.setType(0);
        setAlarms.add(setAlarm);
        SetAlarm setAlarm1 = new SetAlarm();
        setAlarm1.setCycle("0+1+2");
        setAlarm1.setTime("10:20");
        setAlarm1.setFlag(1);
        setAlarm1.setType(0);
        setAlarms.add(setAlarm1);
        AppProtocol protocol = new AppProtocol();
        protocol.setImei("35380100360180");
        protocol.setCmdName(MsgTypeEnum.APP_SET_ALARM_CLOCK.getCmdName());
        protocol.setData(setAlarms);
        System.out.println(JSONObject.toJSONString(protocol));
    }


}
